package com.block.api.controller.externalapi;

import com.alibaba.fastjson.JSONObject;
import com.block.chlunion.model.param.UnionCheckParam;
import com.block.chlunion.model.param.UnionLoginParam;
import com.block.chlunion.service.IChannelUnionService;
import com.block.common.exception.ApiException;
import com.block.common.model.vo.ResultInfoVO;
import com.block.common.util.HttpReturnUtil;
import com.block.db.entity.ChannelInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 联登渠道 对接 相关接口
 */
@RestController
@RequestMapping("apiunion/channel")
@Validated
public class ChannelUnionController {

    @Autowired
    IChannelUnionService channelUnionService;


    @RequestMapping(value = "/checkUser", method = {RequestMethod.POST, RequestMethod.GET})
    public String checkUser(@RequestAttribute(value = "tranData") JSONObject tranData,
                               @RequestAttribute(value = "requestNo") String requestNo,
                               @RequestAttribute(value = "channelInfo") ChannelInfo channelInfo,
                               @RequestAttribute(value = "uri") String uri,
                               @RequestAttribute(value = "ip") String ip) throws ApiException {

        String mobileMd5 = tranData.getString("mobileMd5");
        String orderNo = tranData.getString("orderNo");
        String reqIp = tranData.getString("reqIp");

        // TODO: 2023/6/16 联登文档更新
        UnionCheckParam unionCheckParam = new UnionCheckParam();
        unionCheckParam.setMobileMd5(mobileMd5);
        unionCheckParam.setOrderNo(orderNo);
        unionCheckParam.setReqIp(reqIp);

        ResultInfoVO resultInfoVO = channelUnionService.checkUser(channelInfo, unionCheckParam);
        return HttpReturnUtil.returnJson(uri, requestNo, resultInfoVO);
    }

    @RequestMapping(value = "/unionLogin", method = {RequestMethod.POST, RequestMethod.GET})
    public String unionLogin(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "channelInfo") ChannelInfo channelInfo,
                               @RequestAttribute(value = "requestNo") String requestNo,
                              @RequestAttribute(value = "uri") String uri,
                              @RequestAttribute(value = "ip") String ip) throws ApiException {
        String tradeNo = tranData.getString("tradeNo");
        String mobile = tranData.getString("mobile");
        String loginIp = tranData.getString("loginIp");

        UnionLoginParam unionLoginParam  = new UnionLoginParam();
        unionLoginParam.setTradeNo(tradeNo);
        unionLoginParam.setMobile(mobile);

        ResultInfoVO resultInfoVO = channelUnionService.unionLogin(channelInfo, unionLoginParam);
        return HttpReturnUtil.returnJson(uri, requestNo, resultInfoVO);
    }

}
